/**
 * Created by Panda on 16/6/22
 */
(function () {
    'use strict';

    angular
        .module('com.akxx.aqzyk')
        .service('Tools', Tools);


    /** @ngInject */
    function Tools($q, APP, EMOJI_CODE_INVERT, REGEX, Storage, _File) {

        var self = this;

        var user = Storage.get("user");

        /**
         * 跳转到目标页面的函数
         * 此函数解决一个问题！微信支付的时候，如果不刷新页面跳转，微信获取到的URL会不稳定（具体看当前页面是否强制刷新过）
         * */
        self.jump = function (url) {
            Storage.set("jump", url);
            window.location.href = "jump.html";
        };

        self.isTextareaOverLength = function (text, maxLength, toast) {
            var res = true;
            if (!_.isEmpty(text)) {
                text = text.replace(REGEX.EMOJI_ESCAPE, " ");
                res = text.length <= maxLength;
                if (!res) {
                    //TODO:提示信息
                }
            }
            return res;
        };

        //获取浏览器地址中的参数
        self.getQueryString = function (name) {
            var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
            var query = window.location.href.split("?")[1];
            var context = "";
            if (!_.isEmpty(query)) {
                var r = query.match(reg);  //获取url中"?"符后的字符串并正则匹配
                if (r != null) {
                    context = r[2];
                }
                reg = null;
                r = null;
            }
            return decodeURIComponent(context || "");
        };

        self.analysisState = function (state) {
            var res = {};
            if (_.isEmpty(state)) {
                res.state = "home.index";
                res.param = {};
            }
        };

        //格式化金额
        self.formatMoney = function (s, n) {
            if (isNaN(s)) {
                return "0.00";
            }

            n = n > 0 && n <= 20 ? n : 2;
            s = parseFloat((s + "").replace(/[^\d\.-]/g, "")).toFixed(n) + "";
            var l = s.split(".")[0].split("").reverse(),
                r = s.split(".")[1];
            var t = "";
            for (var i = 0; i < l.length; i++) {
                t += l[i] + ((i + 1) % 3 == 0 && (i + 1) != l.length ? "," : "");
            }
            return t.split("").reverse().join("") + "." + r;
        };

        //从数组中删除指定下标的元素
        self.removeItem = function (arr, index) {
            if (_.isEmpty(arr)) {
                return [];
            }
            var tempArr = [];
            for (var i = 0; i < arr.length; i++) {
                if (i !== index) {
                    tempArr.push(arr[i]);
                }
            }
            return tempArr;
        };

        //根据属性值从数组中删除对象
        self.removeItemWithProperty = function (arr, filedName, value) {
            if (_.isEmpty(arr)) {
                return [];
            }
            var tempArr = [];
            angular.forEach(arr, function (item) {
                if (item[filedName] !== value) {
                    tempArr.push(item);
                }
            });
            return tempArr;
        };


        self.formatDate = function (date, format) {
            if (_.isEmpty(format)) {
                format = "YYYY-MM-DD HH:mm:ss";
            }
            return moment(date).format(format);
        }

    }

})();
